class GridEvent {
  static onTopMouseDown = (event) => {
    event.stopPropagation()
    cursor.set('row')
    __position = new Position('top')
    __position.setStart(event)
    __position.setGrid(__grid)
    GridEvent.bindMouseMove()
  }

  static onRightMouseDown = (event) => {
    event.stopPropagation()
    cursor.set('column')
    __position = new Position('right')
    __position.setStart(event)
    __position.setGrid(__grid)
    GridEvent.bindMouseMove()
  }

  static onBottomMouseDown = (event) => {
    event.stopPropagation()
    cursor.set('row')
    __position = new Position('bottom')
    __position.setStart(event)
    __position.setGrid(__grid)
    GridEvent.bindMouseMove()
  }

  static onLeftMouseDown = (event) => {
    event.stopPropagation()
    cursor.set('column')
    __position = new Position('left')
    __position.setStart(event)
    __position.setGrid(__grid)
    GridEvent.bindMouseMove()
  }

  static bindMouseMove = () => {
    $(document)
        .off('mousemove', GridEvent.onMouseMove)
        .on('mousemove', GridEvent.onMouseMove)
    $(document)
        .off('mouseup', GridEvent.onMouseUp)
        .on('mouseup', GridEvent.onMouseUp)
  }

  static offMouseMove = () => {
    // console.log('offMouseMove')
    cursor.remove()
    $(document)
        .off('mousemove', GridEvent.onMouseMove)
    $(document)
        .off('mouseup', GridEvent.onMouseUp)
  }

  static onMouseMove = (event) => {
    // console.log('onMouseMove')
    if (event.which) {
      __position.setMove(event)
    } else {
      GridEvent.offMouseMove()
    }
  }

  static onMouseUp = (event) => {
    __position?.setEnd(event);
    GridEvent.offMouseMove()
  }

  static bind = ($grid) => {
    $container = $grid
    $container.on('mousedown', '.sullivan-grid-item-top', GridEvent.onTopMouseDown)
    $container.on('mousedown', '.sullivan-grid-item-right', GridEvent.onRightMouseDown)
    $container.on('mousedown', '.sullivan-grid-item-bottom', GridEvent.onBottomMouseDown)
    $container.on('mousedown', '.sullivan-grid-item-left', GridEvent.onLeftMouseDown)
  }
}